package cn.core.act.rest;

import lombok.extern.log4j.Log4j2;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * cs REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("cs/act")
public class CsActController {

  @Resource
  private RepositoryService repositoryService;

  @Resource
  private RuntimeService runtimeService;

  @Resource
  private TaskService taskService;

  /**
   * cs
   * http://127.0.0.1:6660/api/cs/act/cs
   * @return 返回
   */
  @GetMapping("cs")
  public String cs() throws Exception {
    try {
//      String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
//          "<bpmn2:definitions xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:bpmn2=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\" xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\" xmlns:activiti=\"http://activiti.org/bpmn\" xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\" id=\"model-c0ffdad6-5aeb-4101-bef1-0d7d9bf0ac64\" name=\"cs-qwe\" targetNamespace=\"http://bpmn.io/schema/bpmn\" xsi:schemaLocation=\"http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd\">\n" +
//          "  <bpmn2:process id=\"Process_L-rcADdZ8\" name=\"cs-qwe\" isExecutable=\"true\">\n" +
//          "    <bpmn2:documentation />\n" +
//          "    <bpmn2:startEvent id=\"Event_1\" name=\"\">\n" +
//          "      <bpmn2:outgoing>SequenceFlow_148q8ed</bpmn2:outgoing>\n" +
//          "    </bpmn2:startEvent>\n" +
//          "    <bpmn2:sequenceFlow id=\"SequenceFlow_148q8ed\" sourceRef=\"Event_1\" targetRef=\"Task_15rymsy\" />\n" +
//          "    <bpmn2:sequenceFlow id=\"SequenceFlow_0wmmow6\" sourceRef=\"Task_15rymsy\" targetRef=\"Task_0nkmta2\" />\n" +
//          "    <bpmn2:userTask id=\"Task_15rymsy\" name=\"1\" activiti:assignee=\"1\" activiti:priority=\"0\">\n" +
//          "      <bpmn2:incoming>SequenceFlow_148q8ed</bpmn2:incoming>\n" +
//          "      <bpmn2:outgoing>SequenceFlow_0wmmow6</bpmn2:outgoing>\n" +
//          "    </bpmn2:userTask>\n" +
//          "    <bpmn2:userTask id=\"Task_0gyife2\" name=\"3\" activiti:formKey=\"\" activiti:candidateGroups=\"3\">\n" +
//          "      <bpmn2:incoming>SequenceFlow_04ay1fj</bpmn2:incoming>\n" +
//          "      <bpmn2:outgoing>SequenceFlow_0kq3zqr</bpmn2:outgoing>\n" +
//          "    </bpmn2:userTask>\n" +
//          "    <bpmn2:endEvent id=\"EndEvent_120y08n\">\n" +
//          "      <bpmn2:incoming>SequenceFlow_0kq3zqr</bpmn2:incoming>\n" +
//          "    </bpmn2:endEvent>\n" +
//          "    <bpmn2:sequenceFlow id=\"SequenceFlow_0kq3zqr\" sourceRef=\"Task_0gyife2\" targetRef=\"EndEvent_120y08n\" />\n" +
//          "    <bpmn2:sequenceFlow id=\"SequenceFlow_04ay1fj\" sourceRef=\"Task_0nkmta2\" targetRef=\"Task_0gyife2\" />\n" +
//          "    <bpmn2:userTask id=\"Task_0nkmta2\" name=\"2\" activiti:candidateUsers=\"2,22\">\n" +
//          "      <bpmn2:incoming>SequenceFlow_0wmmow6</bpmn2:incoming>\n" +
//          "      <bpmn2:outgoing>SequenceFlow_04ay1fj</bpmn2:outgoing>\n" +
//          "    </bpmn2:userTask>\n" +
//          "  </bpmn2:process>\n" +
//          "  <bpmndi:BPMNDiagram id=\"BPMNDiagram_1\">\n" +
//          "    <bpmndi:BPMNPlane id=\"BPMNPlane_1\" bpmnElement=\"Process_L-rcADdZ8\">\n" +
//          "      <bpmndi:BPMNShape id=\"_BPMNShape_Event_2\" bpmnElement=\"Event_1\">\n" +
//          "        <dc:Bounds x=\"12\" y=\"-98\" width=\"36\" height=\"36\" />\n" +
//          "        <bpmndi:BPMNLabel>\n" +
//          "          <dc:Bounds x=\"19\" y=\"-55\" width=\"22\" height=\"14\" />\n" +
//          "        </bpmndi:BPMNLabel>\n" +
//          "      </bpmndi:BPMNShape>\n" +
//          "      <bpmndi:BPMNEdge id=\"SequenceFlow_148q8ed_di\" bpmnElement=\"SequenceFlow_148q8ed\">\n" +
//          "        <di:waypoint x=\"48\" y=\"-80\" />\n" +
//          "        <di:waypoint x=\"100\" y=\"-80\" />\n" +
//          "      </bpmndi:BPMNEdge>\n" +
//          "      <bpmndi:BPMNEdge id=\"SequenceFlow_0wmmow6_di\" bpmnElement=\"SequenceFlow_0wmmow6\">\n" +
//          "        <di:waypoint x=\"200\" y=\"-80\" />\n" +
//          "        <di:waypoint x=\"260\" y=\"-80\" />\n" +
//          "      </bpmndi:BPMNEdge>\n" +
//          "      <bpmndi:BPMNShape id=\"UserTask_0qrps2w_di\" bpmnElement=\"Task_15rymsy\">\n" +
//          "        <dc:Bounds x=\"100\" y=\"-120\" width=\"100\" height=\"80\" />\n" +
//          "      </bpmndi:BPMNShape>\n" +
//          "      <bpmndi:BPMNShape id=\"UserTask_0odetth_di\" bpmnElement=\"Task_0gyife2\">\n" +
//          "        <dc:Bounds x=\"420\" y=\"-120\" width=\"100\" height=\"80\" />\n" +
//          "      </bpmndi:BPMNShape>\n" +
//          "      <bpmndi:BPMNShape id=\"EndEvent_120y08n_di\" bpmnElement=\"EndEvent_120y08n\">\n" +
//          "        <dc:Bounds x=\"582\" y=\"-98\" width=\"36\" height=\"36\" />\n" +
//          "      </bpmndi:BPMNShape>\n" +
//          "      <bpmndi:BPMNEdge id=\"SequenceFlow_0kq3zqr_di\" bpmnElement=\"SequenceFlow_0kq3zqr\">\n" +
//          "        <di:waypoint x=\"520\" y=\"-80\" />\n" +
//          "        <di:waypoint x=\"582\" y=\"-80\" />\n" +
//          "      </bpmndi:BPMNEdge>\n" +
//          "      <bpmndi:BPMNEdge id=\"SequenceFlow_04ay1fj_di\" bpmnElement=\"SequenceFlow_04ay1fj\">\n" +
//          "        <di:waypoint x=\"360\" y=\"-80\" />\n" +
//          "        <di:waypoint x=\"420\" y=\"-80\" />\n" +
//          "      </bpmndi:BPMNEdge>\n" +
//          "      <bpmndi:BPMNShape id=\"UserTask_0k1thsa_di\" bpmnElement=\"Task_0nkmta2\">\n" +
//          "        <dc:Bounds x=\"260\" y=\"-120\" width=\"100\" height=\"80\" />\n" +
//          "      </bpmndi:BPMNShape>\n" +
//          "    </bpmndi:BPMNPlane>\n" +
//          "  </bpmndi:BPMNDiagram>\n" +
//          "</bpmn2:definitions>\n";
//      Deployment deployment = repositoryService.createDeployment().addString("cs-qwe.bpmn20.xml", s).deploy();
//      repositoryService.deleteDeployment("55e8bb52-a69b-11ec-b71f-acde48001122");
//      Deployment deployment = repositoryService.createDeploymentQuery().deploymentId("99ee58b9-a6d2-11ec-b594-acde48001122").singleResult();
//      System.out.println();
//      ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId("99ee58b9-a6d2-11ec-b594-acde48001122").singleResult();
//      System.out.println();

//      ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Process_L-rcADdZ8");
//      processInstance.getId();
//      List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().processInstanceId("0bf835d7-a6da-11ec-805c-acde48001122").list();
//      System.out.println();

//      ProcessInstance processInstance = runtimeService.startProcessInstanceById("cs:1:e5163cb1-b245-11ec-8241-acde48001122");
//      String s1 = processInstance.getId();
//      System.out.println();
//      List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().processInstanceId("31762307-a6e5-11ec-8180-acde48001122").list();
//      String s3 = processInstances.iterator().next().getId();
//      String s4 = processInstances.iterator().next().getProcessInstanceId();
//      System.out.println();
//      runtimeService.deleteProcessInstance(processInstances.iterator().next().getId(), "撤销流程");
//      System.out.println();

//      List<Task> tasks = taskService.createTaskQuery().taskAssignee("cde30062b34f4ac393b61844d32d1bbc").list();
//      Task task = tasks.iterator().next();
//      taskService.complete(task.getId());

//      List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("2").list();
//      Task task = tasks.iterator().next();
//      taskService.complete(task.getId());

//      List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("57c2fabe67004d2b8087d3d282ec87a9").list();
//      System.out.println();
//      Task task = tasks.iterator().next();
//      taskService.complete(task.getId());
//      System.out.println();
//      List<Task> tasks1 = taskService.createTaskQuery().taskCandidateGroup("3").list();
//      System.out.println();
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return "";
  }

}
